package com.fastwire.carriere.service;

import java.util.List;

import javax.jws.WebService;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.entity.CodeDTO;
import com.fastwire.carriere.common.util.CodeLoopUpUtil;
import com.fastwire.carriere.common.util.ResourceUtil;
import com.fastwire.carriere.controller.ControllerProxy;
import com.fastwire.carriere.controller.UserController;
import com.fastwire.carriere.controller.impl.UserControllerImpl;
import com.fastwire.carriere.service.webui.exception.ServiceException;
import com.fastwire.carriere.user.vo.PermissionVo;
import com.fastwire.carriere.user.vo.SessionVo;
import com.fastwire.carriere.user.vo.UserGroupVo;
import com.fastwire.carriere.user.vo.UserVo;
import com.google.common.collect.Lists;

// TODO: Auto-generated Javadoc
/**
 * The Class UserServiceImpl.
 */
@WebService(endpointInterface = "com.fastwire.carriere.service.UserService")
public class UserServiceImpl implements UserService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#authenticateUser(java.lang.
	 * String, java.lang.String)
	 */
	@Override
	public SessionVo login(UserVo userVo) throws ServiceException {

		UserController controller = new UserControllerImpl();

		try {
			return controller.login(userVo);

		} catch (Exception e) {
			e.printStackTrace();

			throw new ServiceException(e.getMessage());
		}

	}

	@Override
	public String logout(SessionVo sessionVo) throws ServiceException {

		String msg = Messages.getString("message.common.service.success");

		try {
			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			controller.logout(sessionVo);
			return msg;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#createUserGroup(com.fastwire
	 * .carriere.user.vo.UserGroupVo)
	 */
	@Override
	public String createUserGroup(UserGroupVo userGroup, SessionVo sessionVo)
			throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			controller.createUserGroup(userGroup);
			return msg;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#editUserGroup(com.fastwire.
	 * carriere.user.vo.UserGroupVo)
	 */
	@Override
	public String editUserGroup(UserGroupVo userGroup, SessionVo sessionVo)
			throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			controller.editUserGroup(userGroup);
			return msg;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#createUser(com.fastwire.carriere
	 * .user.vo.UserVo)
	 */
	@Override
	public String createUser(UserVo userVo, SessionVo sessionVo)
			throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			controller.createUser(userVo);
			return msg;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#editUser(com.fastwire.carriere
	 * .user.vo.UserVo)
	 */
	@Override
	public String editUser(UserVo userVo, SessionVo sessionVo)
			throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		try {
			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			controller.editUser(userVo);
			return msg;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.UserService#getAllSystemPermisions()
	 */
	@Override
	public List<PermissionVo> getAllSystemPermisions() throws ServiceException {

		try {
			List<CodeDTO> codes = CodeLoopUpUtil
					.getCodeDTOByCateLog("Permission");

			List<PermissionVo> permissionVos = Lists.newArrayList();
			for (CodeDTO codeDTO : codes) {
				PermissionVo permissionVo = new PermissionVo();
				permissionVo.setName(codeDTO.getName());
				permissionVo.setAction("access");
				permissionVos.add(permissionVo);
			}

			return permissionVos;
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#getUserbySearchCriteria(com
	 * .fastwire.carriere.user.vo.UserVo)
	 */
	@Override
	public List<UserVo> getUserbySearchCriteria(UserVo userVo,
			SessionVo sessionVo) throws ServiceException {

		try {
			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			List userVos = controller.searchUser(userVo,
					sessionVo.getUserName());
			return userVos;

		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#getUserGroupbySearchCriteria
	 * (com.fastwire.carriere.user.vo.UserGroupVo)
	 */
	@Override
	public List<UserGroupVo> getUserGroupbySearchCriteria(UserGroupVo searchVo,
			SessionVo sessionVo) throws ServiceException {

		try {

			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			List userGroupVos = controller.searchUserGroup(searchVo);
			return userGroupVos;

		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#deleteUser(java.lang.String)
	 */
	@Override
	public String deleteUser(String userId, SessionVo sessionVo)
			throws ServiceException {

		String msg = Messages.getString("message.common.service.success");

		try {

			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			long id = Long.parseLong(userId);

			controller.deleteUser(id);
			return msg;

		} catch (Exception e) {

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.service.UserService#deleteUserGroup(java.lang.String
	 * )
	 */
	@Override
	public String deleteUserGroup(String userGroupId, SessionVo sessionVo)
			throws ServiceException {

		String msg = Messages.getString("message.common.service.success");

		try {

			UserController controller = (UserController) ResourceUtil
					.getSessionBindClassByName(
							UserControllerImpl.class.getName(), sessionVo, true);
			long id = Long.parseLong(userGroupId);

			controller.deleteUserGroup(id);
			return msg;

		} catch (Exception e) {

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}
	}
}
